home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Sound / LAME / WarpOS / src / Dll / Example.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-20  |  7.5 KB  |  281 lines

  1. /*
  2.  *    LAME DLL Sample Code.
  3.  *
  4.  *    Copyright (c) 2000 A.L. Faber
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  * 
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the
  18.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.  * Boston, MA  02111-1307, USA.
  20.  */
  21.  
  22.  
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include "BladeMP3EncDLL.h"
  29.  
  30. BEINITSTREAM        beInitStream=NULL;
  31. BEENCODECHUNK        beEncodeChunk=NULL;
  32. BEDEINITSTREAM        beDeinitStream=NULL;
  33. BECLOSESTREAM        beCloseStream=NULL;
  34. BEVERSION            beVersion=NULL;
  35. BEWRITEVBRHEADER    beWriteVBRHeader=NULL;
  36.  
  37.  
  38. // Main program
  39. int main(int argc, char *argv[])
  40. {    
  41.     HINSTANCE    hDLL            =NULL;
  42.     FILE*        pFileIn            =NULL;
  43.     FILE*        pFileOut        =NULL;
  44.     BE_VERSION    Version            ={0,};
  45.     BE_CONFIG    beConfig        ={0,};
  46.  
  47.     CHAR        strFileIn[255]    ={'0',};
  48.     CHAR        strFileOut[255]    ={'0',};
  49.  
  50.     DWORD        dwSamples        =0;
  51.     DWORD        dwMP3Buffer        =0;
  52.     HBE_STREAM    hbeStream        =0;
  53.     BE_ERR        err                =0;
  54.  
  55.     PBYTE        pMP3Buffer        =NULL;
  56.     PSHORT        pWAVBuffer        =NULL;
  57.  
  58.     // check number of arguments
  59.     if(argc != 2)
  60.     {
  61.         fprintf(stderr,"Usage: %s <filename.wav>\n", argv[0]);
  62.         fprintf(stderr,"Descr: Short demo to show how to use the lame_enc.dll library file\n");
  63.         fprintf(stderr,"Note : WAV file is assumed to to have the following parameters\n");
  64.         fprintf(stderr,"     : 44100 Hz, stereo, 16 Bits per sample\n");
  65.         return -1;
  66.     }
  67.  
  68.     // Setup the file names
  69.     strcpy(strFileIn ,argv[1]);
  70.     strcpy(strFileOut,argv[1]);
  71.  
  72.     // Add mp3 extention
  73.     strcat(strFileOut,".mp3");
  74.  
  75.     // Load lame_enc.dll library (Make sure though that you set the 
  76.     // project/settings/debug Working Directory correctly, otherwhise the DLL can't be loaded
  77.  
  78.     hDLL=LoadLibrary(".\\Debug\\lame_enc.dll");
  79.  
  80. #ifdef _DEBUG
  81.     hDLL=LoadLibrary(".\\Debug\\lame_enc.dll");
  82. #else
  83.     hDLL=LoadLibrary(".\\Release\\lame_enc.dll");
  84. #endif
  85.  
  86.     if(hDLL==NULL)
  87.     {
  88.         fprintf(stderr,"Error loading lame_enc.DLL");
  89.         return -1;
  90.     }
  91.  
  92.     // Get Interface functions
  93.     beInitStream    = (BEINITSTREAM) GetProcAddress(hDLL, TEXT_BEINITSTREAM);
  94.     beEncodeChunk    = (BEENCODECHUNK) GetProcAddress(hDLL, TEXT_BEENCODECHUNK);
  95.     beDeinitStream    = (BEDEINITSTREAM) GetProcAddress(hDLL, TEXT_BEDEINITSTREAM);
  96.     beCloseStream    = (BECLOSESTREAM) GetProcAddress(hDLL, TEXT_BECLOSESTREAM);
  97.     beVersion        = (BEVERSION) GetProcAddress(hDLL, TEXT_BEVERSION);
  98.     beWriteVBRHeader= (BEWRITEVBRHEADER) GetProcAddress(hDLL,TEXT_BEWRITEVBRHEADER);
  99.  
  100.     // Check if all interfaces are present
  101.     if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion || !beWriteVBRHeader)
  102.     {
  103.  
  104.         printf("Unable to get LAME interfaces");
  105.         return -1;
  106.     }
  107.  
  108.     // Get the version number
  109.     beVersion(&Version);
  110.  
  111.     printf(
  112.             "lame_enc.dll version %u.%02u (%u/%u/%u)\n"
  113.             "lame_enc Engine %u.%02u\n"
  114.             "lame_enc homepage at %s\n\n",    
  115.             Version.byDLLMajorVersion, Version.byDLLMinorVersion,
  116.             Version.byDay, Version.byMonth, Version.wYear,
  117.             Version.byMajorVersion, Version.byMinorVersion,
  118.             Version.zHomepage);
  119.  
  120.     // Try to open the WAV file, be sure to open it as a binary file!    
  121.     pFileIn = fopen(strFileIn,"rb");
  122.  
  123.     // Check file open result
  124.     if(pFileIn == NULL)
  125.     {
  126.         fprintf(stderr,"Error opening %s", argv[1]);
  127.         return -1;
  128.     }
  129.  
  130.     // Open MP3 file
  131.     pFileOut= fopen(strFileOut,"wb+");
  132.  
  133.     // Check file open result
  134.     if(pFileOut == NULL)
  135.     {
  136.         fprintf(stderr,"Error creating file %s", strFileOut);
  137.         return -1;
  138.     }
  139.  
  140.     memset(&beConfig,0,sizeof(beConfig));                    // clear all fields
  141.  
  142.     // use the LAME config structure
  143.     beConfig.dwConfig = BE_CONFIG_LAME;
  144.  
  145.     // this are the default settings for testcase.wav
  146.     beConfig.format.LHV1.dwStructVersion    = 1;
  147.     beConfig.format.LHV1.dwStructSize        = sizeof(beConfig);        
  148.     beConfig.format.LHV1.dwSampleRate        = 44100;                // INPUT FREQUENCY
  149.     beConfig.format.LHV1.dwReSampleRate        = 0;                    // DON"T RESAMPLE
  150.     beConfig.format.LHV1.nMode                = BE_MP3_MODE_JSTEREO;    // OUTPUT IN STREO
  151.     beConfig.format.LHV1.dwBitrate            = 128;                    // MINIMUM BIT RATE
  152.     beConfig.format.LHV1.nPreset            = LQP_HIGH_QUALITY;        // QUALITY PRESET SETTING
  153.     beConfig.format.LHV1.dwMpegVersion        = MPEG1;                // MPEG VERSION (I or II)
  154.     beConfig.format.LHV1.dwPsyModel            = 0;                    // USE DEFAULT PSYCHOACOUSTIC MODEL 
  155.     beConfig.format.LHV1.dwEmphasis            = 0;                    // NO EMPHASIS TURNED ON
  156.     beConfig.format.LHV1.bOriginal            = TRUE;                    // SET ORIGINAL FLAG
  157.  
  158. //    beConfig.format.LHV1.dwMaxBitrate        = 320;                    // MAXIMUM BIT RATE
  159. //    beConfig.format.LHV1.bCRC                = TRUE;                    // INSERT CRC
  160. //    beConfig.format.LHV1.bCopyright            = TRUE;                    // SET COPYRIGHT FLAG    
  161. //    beConfig.format.LHV1.bPrivate            = TRUE;                    // SET PRIVATE FLAG
  162. //    beConfig.format.LHV1.bWriteVBRHeader    = TRUE;                    // YES, WRITE THE XING VBR HEADER
  163. //    beConfig.format.LHV1.bEnableVBR            = TRUE;                    // USE VBR
  164. //    beConfig.format.LHV1.nVBRQuality        = 5;                    // SET VBR QUALITY
  165.     beConfig.format.LHV1.bNoRes                = TRUE;                    // No Bit resorvoir
  166.  
  167. // Preset Test
  168. //    beConfig.format.LHV1.nPreset            = LQP_PHONE;
  169.  
  170.     // Init the MP3 Stream
  171.     err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);
  172.  
  173.     // Check result
  174.     if(err != BE_ERR_SUCCESSFUL)
  175.     {
  176.         fprintf(stderr,"Error opening encoding stream (%lu)", err);
  177.         return -1;
  178.     }
  179.  
  180.  
  181.     // Allocate MP3 buffer
  182.     pMP3Buffer = new BYTE[dwMP3Buffer];
  183.  
  184.     // Allocate WAV buffer
  185.     pWAVBuffer = new SHORT[dwSamples];
  186.  
  187.     // Check if Buffer are allocated properly
  188.     if(!pMP3Buffer || !pWAVBuffer)
  189.     {
  190.         printf("Out of memory");
  191.         return -1;
  192.     }
  193.  
  194.     DWORD dwRead=0;
  195.     DWORD dwWrite=0;
  196.     DWORD dwDone=0;
  197.     DWORD dwFileSize=0;
  198.  
  199.     // Seek to end of file
  200.     fseek(pFileIn,0,SEEK_END);
  201.  
  202.     // Get the file size
  203.     dwFileSize=ftell(pFileIn);
  204.  
  205.     // Seek back to start of WAV file,
  206.     // but skip the first 44 bytes, since that's the WAV header
  207.     fseek(pFileIn,44,SEEK_SET);
  208.  
  209.  
  210.     // Convert All PCM samples
  211.     while ( (dwRead=fread(pWAVBuffer,sizeof(SHORT),dwSamples,pFileIn)) >0 )
  212.     {
  213.         // Encode samples
  214.         err = beEncodeChunk(hbeStream, dwRead, pWAVBuffer, pMP3Buffer, &dwWrite);
  215.  
  216.         // Check result
  217.         if(err != BE_ERR_SUCCESSFUL)
  218.         {
  219.             beCloseStream(hbeStream);
  220.             fprintf(stderr,"beEncodeChunk() failed (%lu)", err);
  221.             return -1;
  222.         }
  223.         
  224.         // write dwWrite bytes that are returned in tehe pMP3Buffer to disk
  225.         if(fwrite(pMP3Buffer,1,dwWrite,pFileOut) != dwWrite)
  226.         {
  227.             fprintf(stderr,"Output file write error");
  228.             return -1;
  229.         }
  230.  
  231.         dwDone += dwRead*sizeof(SHORT);
  232.  
  233.         printf("Done: %0.2f%%     \r", 100 * (float)dwDone/(float)(dwFileSize));
  234.     }
  235.  
  236.     // Deinit the stream
  237.     err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
  238.  
  239.     // Check result
  240.     if(err != BE_ERR_SUCCESSFUL)
  241.     {
  242.  
  243.         beCloseStream(hbeStream);
  244.         fprintf(stderr,"beExitStream failed (%lu)", err);
  245.         return -1;
  246.     }
  247.  
  248.     // Are there any bytes returned from the DeInit call?
  249.     // If so, write them to disk
  250.     if(dwWrite)
  251.     {
  252.         if(fwrite(pMP3Buffer,1,dwWrite,pFileOut) != dwWrite)
  253.         {
  254.             fprintf(stderr,"Output file write error");
  255.             return -1;
  256.         }
  257.     }
  258.  
  259.     // close the MP3 Stream
  260.     beCloseStream(hbeStream);
  261.  
  262.     // Delete WAV buffer
  263.     delete [] pWAVBuffer;
  264.  
  265.     // Delete MP3 Buffer
  266.     delete [] pMP3Buffer;
  267.  
  268.     // Close input file
  269.     fclose(pFileIn);
  270.  
  271.     // Close output file
  272.     fclose(pFileOut);
  273.  
  274.     // Write the VBR Tag
  275.     beWriteVBRHeader(strFileOut);
  276.  
  277.     // Were done, return OK result
  278.     return 0;
  279. }
  280.  
  281.